unit PingPongPlayer;

{$IFDEF FPC}
  {$MODE Delphi}
{$ENDIF}

interface

uses Sysutils, Windows, Actors;

type
  TPingPong = class (TActor)
  protected
    Id: string;
    Ticks: Cardinal;
    Rounds: Integer;
  public
    procedure Init(const msg: Variant); override;
    procedure Ping(const round:Variant);
    procedure Pong(const round:Variant);
  end;

implementation

procedure TPingPong.Init(const msg:Variant);
begin
  Id := msg[0];
  Rounds := msg[1];
  Ticks := GetTickCount;
  Send(Ping, Rounds);
end;

procedure TPingPong.Ping(const round: Variant);
var
  t:Integer;
begin { Future; }
  if round > 0 then begin
    Send(Pong, round);
  end else begin
    t := GetTickCount-Ticks+1;
    WriteLn(Format('%s:PingPong(%d): %d (%0.n Rounds/sec)', [id, Rounds, t, (Rounds*1000/t)]));
  end;
end;

procedure TPingPong.Pong(const round: Variant);
begin { Future; }
  Send(Ping, round-1);
end;

end.

